package com.lizhi.menu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lizhi.menu.Dto.CommodityDto;
import com.lizhi.menu.Dto.IdListDto;
import com.lizhi.menu.common.response.Result;
import com.lizhi.menu.domain.Commodity;
import com.lizhi.menu.service.ICommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 商品Controller
 *
 * @author lizhi
 * @date 2023-05-18
 */
@RestController
@RequestMapping("/system/commodity")
@RequiredArgsConstructor
@Api(tags = "商品管理")
public class CommodityController {
    private final ICommodityService commodityService;

    /**
     * 查询商品列表
     */
    @GetMapping("/list")
    @ApiOperation("查询商品列表")
    public Result<Page<Commodity>> list(CommodityDto commodity) {
        return Result.ok(commodityService.selectCommodityList(commodity));
    }


    /**
     * 获取商品详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取商品详细信息")
    public Result<Commodity> getInfo(@PathVariable("id") String id) {
        return Result.ok(commodityService.selectCommodityById(id));
    }

    /**
     * 新增商品
     */
    @PostMapping
    @ApiOperation("新增商品")
    public Result<Void> add(@RequestBody Commodity commodity) {
        commodityService.insertCommodity(commodity);
        return Result.ok();
    }

    /**
     * 修改商品
     */
    @PutMapping
    @ApiOperation("修改商品")
    public Result<Void> edit(@RequestBody Commodity commodity) {
        commodityService.updateCommodity(commodity);
        return Result.ok();
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/remove")
    @ApiOperation("删除商品")
    public Result<Void> remove(@RequestBody IdListDto ids) {
        commodityService.remove(ids.getIdList());
        return Result.ok();
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除商品")
    public Result<Void> remove(@PathVariable String ids) {
        commodityService.remove(ids);
        return Result.ok();
    }
}
